<?php

$flag = true;
$array = null;

include('config.php');
if(isset($_POST['register'])){

	$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
	if(@$_POST['email'] != ""){
		if(@!eregi($exp,@$_POST['email'])){
			@$_POST['email'] = "";
			$err =  "Invalid format for email field";
			$flag = false;
		}
		$getdata = Users::findUser($_POST['email']);
		if($getdata['user_id'] != NULL){
			@$_POST['email'] = "";
			$err =  "This email address already exist";
			$flag = false;
		}
	}else{
		$err =  "Please fill in the email field";
		$flag = false;
	}
	$array[] = @$err;

	$email = @$_POST['email'];
	$pass = @$_POST['pass'];
	$confirm = @$_POST['confirm'];
	if (@$_POST['confirm'] != "" && @$_POST['pass'] !="" ) {
		if (strcmp($pass,$confirm) != 0){
			@$_POST['confirm']="";
			@$_POST['pass'] ="";
			@mysql_close();
			$err =  "Password & Confirm password don not match";
			$flag = false;
		}
	} else {
		@$_POST['confirm']="";
		@$_POST['pass'] ="";
		@mysql_close();
		$err =  "Please fill in the password fields";
		$flag = false;
	}
	$array[] = @$err;
	if ($flag != false){
		//register user
		$adddata = Users::addUser($_POST['email'],$_POST['pass']);
		//echo $_SESSION['userdata']."HELLO!!";
		if($adddata !== TRUE){
			$err =  "Your registration was not completed!";
			$array[] = @$err;
		}
		sendMail($_POST['email'],$_POST['pass']);
	    header('Location: member-home.php');
	}
	
function sendMail($email,$pass){
	$message="";
	$message=$message . 'Thank you for registering with Port Authority\'s <b>goMobile Service</b><br/><Br/>';
	$message=$message . 'Your Login info is:<Br><Br>';
	$message=$message . "<b>Username: " . $email . "<Br>";
	$message=$message . "<b>Password: " . $pass . "<Br>";
	$message=$message . "You can login to your account and start saving Trips & Bus Alerts for all buses run by Port Authority. You can also view real time data about bus locations, arrival times and detour news from our complete fleet of buses anytime on your mobile device<Br>";
	$message=$message . "<Br><br>Thank you for registering!<Br>";
	$message=$message . "Port Authority<Br>";
	
//	echo $message;
	
	$subject = "Account Created | Port Authority - goMobile ";
	$headers = "From: Port Authority <account@portauthority.org> \n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1";
	
	mail($email_id,$subject,$message,$headers);
	echo "mail sent";
}
}
?>